package com.ruoyi.background.service.impl;

import com.ruoyi.background.domain.AppGradeRecord;
import com.ruoyi.background.domain.AppRankUser;
import com.ruoyi.background.domain.AppWechatStep;
import com.ruoyi.background.domain.AppWechatUser;
import com.ruoyi.background.mapper.AppWechatStepMapper;
import com.ruoyi.background.service.IAppGradeRecordService;
import com.ruoyi.background.service.IAppWechatStepService;
import com.ruoyi.background.service.IAppWechatUserService;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 用户微信步数记录Service业务层处理
 *
 * @author susu
 * @date 2024-08-20
 */
@Service
public class AppWechatStepServiceImpl implements IAppWechatStepService {
    @Autowired
    private AppWechatStepMapper appWechatStepMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private IAppWechatUserService wechatUserService;

    @Autowired
    private IAppGradeRecordService gradeRecordService;

    /**
     * 查询用户微信步数记录
     *
     * @param id 用户微信步数记录主键
     * @return 用户微信步数记录
     */
    @Override
    public AppWechatStep selectAppWechatStepById(String id) {
        return appWechatStepMapper.selectAppWechatStepById(id);
    }

    /**
     * 查找今天的记录
     *
     * @return 用户今天的记录
     */
    @Override
    public AppWechatStep getTodayStepRowByUserId() {
        return appWechatStepMapper.getTodayStepRowByUserId(SecurityUtils.getUserId());
    }

    /**
     * 查询用户微信步数记录列表
     *
     * @param appWechatStep 用户微信步数记录
     * @return 用户微信步数记录
     */
    @Override
    public List<AppWechatStep> selectAppWechatStepList(AppWechatStep appWechatStep) {
        return appWechatStepMapper.selectAppWechatStepList(appWechatStep);
    }

    /**
     * 新增用户微信步数记录
     *
     * @param appWechatStep 用户微信步数记录
     * @return 结果
     */
    @Override
    public int insertAppWechatStep(AppWechatStep appWechatStep) {
        appWechatStep.setCreateTime(DateUtils.getNowDate());
        appWechatStep.setUpdateBy(SecurityUtils.getUsername());
        appWechatStep.setCreateBy(SecurityUtils.getUsername());
        appWechatStep.setUpdateTime(DateUtils.getNowDate());
        return appWechatStepMapper.insertAppWechatStep(appWechatStep);
    }

    /**
     * 修改用户微信步数记录
     *
     * @param appWechatStep 用户微信步数记录
     * @return 结果
     */
    @Override
    public int updateAppWechatStep(AppWechatStep appWechatStep) {
        appWechatStep.setUpdateTime(DateUtils.getNowDate());
        appWechatStep.setUpdateBy(SecurityUtils.getUsername());
        return appWechatStepMapper.updateAppWechatStep(appWechatStep);
    }

    /**
     * @param id   记录id
     * @param step 步数
     * @return 结果
     */
    @Override
    public Integer updateTodayStepById(Long id, Integer step) {
        return appWechatStepMapper.updateTodayStepById(id, step);
    }

    /**
     * 批量删除用户微信步数记录
     *
     * @param ids 需要删除的用户微信步数记录主键
     * @return 结果
     */
    @Override
    public int deleteAppWechatStepByIds(String[] ids) {
        return appWechatStepMapper.deleteAppWechatStepByIds(ids);
    }

    /**
     * 删除用户微信步数记录信息
     *
     * @param id 用户微信步数记录主键
     * @return 结果
     */
    @Override
    public int deleteAppWechatStepById(String id) {
        return appWechatStepMapper.deleteAppWechatStepById(id);
    }

    /**
     * 累计天数
     *
     * @return int
     */
    @Override
    public Integer getUserCountRow() {
        Integer s = appWechatStepMapper.getUserCountRow(SecurityUtils.getUserId());
        return ObjectUtils.isEmpty(s) ? 0 : s;
    }

    /**
     * 累计步数
     *
     * @return int
     */
    @Override
    public Integer getUserSumStep() {
        Integer s = appWechatStepMapper.getUserSumStep(SecurityUtils.getUserId());
        return ObjectUtils.isEmpty(s) ? 0 : s;
    }

    /**
     * 累计兑换步数
     *
     * @return int
     */
    @Override
    public Integer getUserSumExchangeStep() {
        Integer s = appWechatStepMapper.getUserSumExchangeStep(SecurityUtils.getUserId());
        return ObjectUtils.isEmpty(s) ? 0 : s;
    }

    /**
     * 用户步数兑换积分
     *
     * @return 结果 兑换的积分
     */
    @Override
    @Transactional
    public Integer userExchangeStep() {
        //判断
        AppWechatStep wechatStep = appWechatStepMapper.getTodayStepRowByUserId(SecurityUtils.getUserId());
        if (ObjectUtils.isEmpty(wechatStep)) throw new RuntimeException("暂无您今日微信步数记录，请稍后再试！");
        int maxStep = Integer.parseInt(configService.selectConfigByKey("app.step.max"));
        //判断是否已经到了最大的兑换步数
        if (wechatStep.getExchangeStep() >= maxStep)
            throw new RuntimeException("您今日已达最大兑换步数：" + maxStep);
        int wechat = Math.min(wechatStep.getWechatStep(),maxStep);
        int canExchangeStep = wechat - wechatStep.getExchangeStep();
        int minStep = Integer.parseInt(configService.selectConfigByKey("app.min.exchange.step"));
        if (canExchangeStep < minStep)
            throw new RuntimeException("您可兑换步数为：" + canExchangeStep + "步，小于" + minStep + "步，无法兑换!");
        int grade = Integer.parseInt(configService.selectConfigByKey("app.exchange.step.grade"));
        //计算可兑换的积分
        int exchange = canExchangeStep / minStep; //满足几个兑换的
        int changeGrade = exchange * grade; //本此兑换的积分
        int changeStep = exchange * minStep; //本次兑换的步数
        //修改用户的 今天的记录
        AppWechatStep update = new AppWechatStep();
        update.setId(wechatStep.getId());
        update.setExchangeStep(wechatStep.getExchangeStep() + changeStep);
        update.setUpdateTime(DateUtils.getNowDate());
        update.setUpdateBy(SecurityUtils.getUsername());
        appWechatStepMapper.updateAppWechatStep(update);
        //修改用户的积分
        AppWechatUser wechatUser = wechatUserService.selectAppWechatByUserId();
        AppWechatUser updateUser = new AppWechatUser();
        updateUser.setId(wechatUser.getId());
        updateUser.setGrade(wechatUser.getGrade() + changeGrade);
        wechatUserService.updateAppWechatUser(updateUser);
        //增加积分记录
        AppGradeRecord gradeRecord = new AppGradeRecord();
        gradeRecord.setUserId(SecurityUtils.getUserId());
        gradeRecord.setType(0);
        gradeRecord.setGrade(changeGrade);
        gradeRecord.setBeforeGrade(wechatUser.getGrade());
        gradeRecord.setCreateTime(DateUtils.getNowDate());
        gradeRecord.setRemark(changeStep + "步兑换奖励");
        gradeRecordService.insertAppGradeRecord(gradeRecord);
        return changeGrade;
    }

    /**
     * 查询今日兑换的排行榜
     *
     * @return list
     */
    @Override
    public List<AppRankUser> getTodayExchangeStep() {
        return appWechatStepMapper.getTodayExchangeStep();
    }

    /**
     * 获取用户今日的排名
     *
     * @return 结果
     */
    @Override
    public Long getUserTodayRank() {
        Long userTodayRank = appWechatStepMapper.getUserTodayRank(SecurityUtils.getUserId());
        if (ObjectUtils.isEmpty(userTodayRank))
            userTodayRank = 9999L;
        return userTodayRank;
    }
}
